package com.docking.heylo.service.impl;

import cn.hutool.http.HttpUtil;
import com.docking.heylo.ThreadContext;
import com.docking.heylo.dao.entity.BigVApply;
import com.docking.heylo.dao.entity.PersonalInformation;
import com.docking.heylo.dao.mapper.BigVApplyMapper;
import com.docking.heylo.dao.mapper.PersonalInformationMapper;
import com.docking.heylo.service.IBigAService;
import com.docking.heylo.web.dto.BigADto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class BigAServiceImpl implements IBigAService {

    @Resource
    private BigVApplyMapper bigVApplyMapper;

    @Resource
    private PersonalInformationMapper personalInformationMapper;

    @Value("${api.url}")
    private String apiUrl;


    @Override
    public PageInfo<Map<String, Object>> applicationRecord(BigADto bigADto) {
        PageHelper.startPage(bigADto.getPageNumber(), bigADto.getSize());
        return new PageInfo<>(bigVApplyMapper.applicationRecord(bigADto.getStatus()));
    }

    @Override
    public PageInfo<Map<String, Object>> bigAList(BigADto bigADto) {
        PageHelper.startPage(bigADto.getPageNumber(), bigADto.getSize());
        return new PageInfo<>(bigVApplyMapper.selectBigList());
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void audit(BigADto bigADto) {
        BigVApply apply = bigVApplyMapper.selectByPrimaryKey(bigADto.getId());

        bigVApplyMapper.updateByPrimaryKeySelective(new BigVApply(){{
            setId(bigADto.getId());
            setStatus(bigADto.getStatus());
            setDenialReason(bigADto.getDenialReason());
            setUpdAt(new Date());
            setUpdBy(ThreadContext.userId());
        }});

        if(bigADto.getStatus() == 2){
            personalInformationMapper.updateByPrimaryKeySelective(new PersonalInformation(){{
                setId(apply.getUserId());
                setV((byte)1);
            }});
        }

        HttpUtil.post(apiUrl + "/public/bigA/audit", new HashMap<String, Object>(){{
            put("code", "bigA/audit" + bigADto.getId());
            put("userId", apply.getUserId());
        }});

    }


}
